package com.it.zhangjun.arithmetic.递归;

/**
 * 汉诺塔
 *
 * @author jun.zhang6
 * @date 2020/10/26
 */
public class Hanoi {
    public static void main(String[] args) {
        hanoi(3, "A", "B", "C");
    }

    /**
     * 将第i号盘子从from移到to
     */
    public static void move(int i, String from, String to) {
        System.out.println(i + "号盘子：" + from + "->" + to);
    }

    /**
     * 将n个盘子从p1移动到p3
     */
    public static void hanoi(int n, String p1, String p2, String p3) {
        if (n <= 1) {
            move(n, p1, p3);
            return;
        }
        hanoi(n - 1, p1, p3, p2);
        move(n, p1, p3);
        hanoi(n - 1, p2, p1, p3);
    }
}
